package Servlet; /**
 * 项目名称：PostServlet.java
 * 文件名称：PostServlet.java
 * 类的描述：无
 * 创建时间：2025-07-18 09:26:23
 * 创建人：大帅比
 * 版权声明：本文件归 大帅比 所有，未经授权不得进行商业使用。
 */

import java.io.*;
import java.util.List;

import Entity.stu;
import Util.GokJDBCUtil2;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

@WebServlet(name = "PostServlet", value = "/api/testpost")
public class InfoServlet extends HttpServlet {
    public void init() {
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
        int page = 1;
        if (req.getParameter("page") != null) {
            page = Integer.parseInt(req.getParameter("page"));
        }
        if (page < 1) page = 1;
        page = page - 1;
        page = page * 12;
        String sql = "select id,name,age,sex from user order by id desc limit " + page + ",12;";
        List<stu> stus = GokJDBCUtil2.queryAll(sql, stu.class, null);
        req.setAttribute("list", stus);
        req.getRequestDispatcher("/Pages/demo.jsp").forward(req, res);
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String method = req.getMethod();
        if (method.equals("GET")) {
            this.doGet(req, res);
        } else {
            PrintWriter writer = res.getWriter();
            res.setCharacterEncoding("utf-8");
            res.setContentType("application/json");
            writer.println("{\"code\":\"400\",\"msg\":\"此方法不允许\"}");
            writer.close();
        }
    }

    public void destroy() {
    }
}